"""
Gua框架日志系统最简使用示例
基于Linus设计哲学：零配置，开箱即用
"""

from gua import GuaApp, LoggingMiddleware

# 创建应用
app = GuaApp()

# 只需要一个中间件 - 零配置
app.use(LoggingMiddleware())


@app.route("/")
async def home():
    """首页 - 自动记录请求日志"""
    return {"message": "Hello World"}


@app.route("/api/users")
async def users():
    """API端点 - 自动记录请求和响应"""
    return {"users": ["Alice", "Bob", "Charlie"]}


@app.websocket("/ws")
async def websocket_example(scope, receive, send):
    """WebSocket - 自动记录连接和消息"""
    message = await receive()
    if message["type"] == "websocket.connect":
        await send({"type": "websocket.accept"})

    # 发送一条消息
    await send({"type": "websocket.send", "text": "Hello from WebSocket!"})

    # 关闭连接
    await send({"type": "websocket.close"})


if __name__ == "__main__":
    import subprocess
    import sys
    
    # 使用gua命令显示示例启动信息
    result = subprocess.run([sys.executable, "-m", "gua.cli", "examples.simple_logging:app", "--example-info"],
                          capture_output=True, text=True)
    if result.returncode == 0:
        print(result.stdout)
    else:
        print("错误: 无法显示启动信息")
        print("请手动运行: gua examples.simple_logging:app --example-info")
